package com.supermarket.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.supermarket.entity.Goods;
import com.supermarket.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController//返回json类型的数据
@RequestMapping("goods")//一级访问路径
public class GoodsController {

    @Autowired//自动装配，将spring容器中的PatientService类型对象，赋值给patientService
    private GoodsService goodsService;


    //医护人员查找


    @RequestMapping("findGoodsAll")
    public Map findGoodsAll(){
        List<Goods> goodsList = goodsService.findGoodsAll();

        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",1000);
        map.put("data",goodsList);
        System.out.println(map);
        return map;
    }


    @RequestMapping("findByGoodsPage")
    public Map findByGoodsPage(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit",required = false,defaultValue = "3") Integer pageSize
    ){



        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //紧跟在PageHelper.startPage（）后才会被分页
        List<Goods> goodsList = goodsService.findGoodsAll();

        //创建PageInfo对象
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);

        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",pageInfo.getTotal());//总记录数
        map.put("data",pageInfo.getList());//分页数据
        System.out.println(map);
        return map;
    }

    @RequestMapping("add")
    public void add(Goods goods, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //System.out.println(patient);
        goodsService.add(goods);

        //添加完之后，跳转到首页
        response.sendRedirect(request.getContextPath() + "/home.html");
    }
    @RequestMapping("deleteById")
    public String deleteById(String id){
        //System.out.println(id);
        goodsService.deleteById(id);
        return "success";
    }

    @RequestMapping("updateGoods")
    public String updateGoods(Goods goods){

        //System.out.println(patient);
        goodsService.updateGoods(goods);

        return "success";
    }

    @RequestMapping("batchDel")
    public String batchDel(Integer [] ids){
        //判断
        if (ids.length>0){
            goodsService.batchDel(ids);
        }
        return "success";
    }

    @RequestMapping("wirteExcel")
    public void wirteExcel(Integer [] ids,HttpServletResponse response) throws IOException {
        //System.out.println(Arrays.toString(ids));

        if(ids.length > 0){
            List<Goods> goodsList = goodsService.findByIds(ids);
            System.out.println(goodsList);

            System.out.println("系统时间：" + System.currentTimeMillis() );

            String time = new SimpleDateFormat("yyyy-MM-dd ").format(new Date());
            System.out.println(time);


            //以下载的方式打开
            String fileName = "goods" + time + ".xlsx";
            response.setHeader("Content-Disposition", "attachment;filename="+fileName);
            //获取输出流
            ServletOutputStream outputStream = response.getOutputStream();
            //流的方式写入
            EasyExcel.write(outputStream, Goods.class).sheet("商品列表").doWrite(goodsList);
        }

    }


    //MultipartFile的参数名必须是：file
    @RequestMapping("upload")
    public Map upload(MultipartFile file){

        HashMap<String, Object> map = new HashMap<>();

        if(file!=null){
            System.out.println(file.getOriginalFilename());//输出上传的文件名

            //调用上传的方法
            goodsService.upload(file);

            map.put("code",0);
            map.put("msg","success");

        }

        return map;
    }

}
